﻿#ifndef ITASK_H
#define ITASK_H

#include <QObject>
#include <QThread>
#include <QAtomicInteger>


/**
 * @brief The ITask class
 * 抽象任务类，必须要实现：
 * void runTask();
 */
class ITask : public QObject
{
    Q_OBJECT
public:
    explicit ITask(QObject *parent = nullptr);
    ~ITask();

    void setTaskName(const QString& name);
    QString taskName() const;

    bool isRunning() const;
    void start();
    void stop();

    static QString INFO_TASK_STOPPED(const QString& taskname);
    static QString WARN_TASK_IS_RUNNING(const QString& taskname);

protected:
    void moveThread(QObject* obj=nullptr);

signals:
    void errorOccurred(const QString&);
    void taskStarted();
    void taskFinished();
    void taskStopped();

private:
    virtual void runTask() = 0;  // 真正执行任务的逻辑
    void run();

private:
    QThread m_taskThread;
    QAtomicInteger<bool> m_running;
    QString m_taskName;
};

#endif // ITASK_H
